{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7aa0e372",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "# 编码器-解码器架构\n",
    "\n",
    "编码器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "198a8db6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-07T16:28:28.614295Z",
     "iopub.status.busy": "2022-12-07T16:28:28.613672Z",
     "iopub.status.idle": "2022-12-07T16:28:29.706051Z",
     "shell.execute_reply": "2022-12-07T16:28:29.705245Z"
    },
    "origin_pos": 2,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [],
   "source": [
    "from torch import nn\n",
    "\n",
    "\n",
    "class Encoder(nn.Module):\n",
    "    \"\"\"编码器-解码器架构的基本编码器接口\"\"\"\n",
    "    def __init__(self, **kwargs):\n",
    "        super(Encoder, self).__init__(**kwargs)\n",
    "\n",
    "    def forward(self, X, *args):\n",
    "        raise NotImplementedError"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf7a21c4",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "解码器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b9ddbe15",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-07T16:28:29.710127Z",
     "iopub.status.busy": "2022-12-07T16:28:29.709619Z",
     "iopub.status.idle": "2022-12-07T16:28:29.714399Z",
     "shell.execute_reply": "2022-12-07T16:28:29.713720Z"
    },
    "origin_pos": 7,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [],
   "source": [
    "class Decoder(nn.Module):\n",
    "    \"\"\"编码器-解码器架构的基本解码器接口\"\"\"\n",
    "    def __init__(self, **kwargs):\n",
    "        super(Decoder, self).__init__(**kwargs)\n",
    "\n",
    "    def init_state(self, enc_outputs, *args):\n",
    "        raise NotImplementedError\n",
    "\n",
    "    def forward(self, X, state):\n",
    "        raise NotImplementedError"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "08ddb2a6",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "合并编码器和解码器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e8af0cfe",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-07T16:28:29.717505Z",
     "iopub.status.busy": "2022-12-07T16:28:29.717103Z",
     "iopub.status.idle": "2022-12-07T16:28:29.722209Z",
     "shell.execute_reply": "2022-12-07T16:28:29.721493Z"
    },
    "origin_pos": 12,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [],
   "source": [
    "class EncoderDecoder(nn.Module):\n",
    "    \"\"\"编码器-解码器架构的基类\"\"\"\n",
    "    def __init__(self, encoder, decoder, **kwargs):\n",
    "        super(EncoderDecoder, self).__init__(**kwargs)\n",
    "        self.encoder = encoder\n",
    "        self.decoder = decoder\n",
    "\n",
    "    def forward(self, enc_X, dec_X, *args):\n",
    "        enc_outputs = self.encoder(enc_X, *args)\n",
    "        dec_state = self.decoder.init_state(enc_outputs, *args)\n",
    "        return self.decoder(dec_X, dec_state)"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "language_info": {
   "name": "python"
  },
  "rise": {
   "autolaunch": true,
   "enable_chalkboard": true,
   "overlay": "<div class='my-top-right'><img height=80px src='http://d2l.ai/_static/logo-with-text.png'/></div><div class='my-top-left'></div>",
   "scroll": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}